package com.ruoyi.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.ruoyi.system.domain.dto.resp.SubDeviceListRespDTO;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.SubDevice;
import com.ruoyi.system.domain.dto.req.AssetAccountReqDTO;
import com.ruoyi.system.domain.dto.resp.SubDeviceTotalCountResp;
import com.ruoyi.system.mapper.SubDeviceMapper;
import com.ruoyi.system.service.SubDeviceService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
public class SubDeviceServiceImpl implements SubDeviceService {

    @Resource
    private SubDeviceMapper subDeviceMapper;

    @Override
    public SubDeviceTotalCountResp queryTotalCount() {
        SubDeviceTotalCountResp subDeviceTotalCountResp = subDeviceMapper.selectCount();
        subDeviceTotalCountResp.setTotalCount(subDeviceTotalCountResp.getOnlineCount() + subDeviceTotalCountResp.getOfflineCount());
        return subDeviceTotalCountResp;
    }



    @Transactional
    @Override
    public void deleteSubDevice(AssetAccountReqDTO reqDTO) {

        SubDevice subDevice = subDeviceMapper.selectByPrimaryKey(reqDTO.getId());
        if (Objects.isNull(subDevice)) {
            return;
        }

        subDeviceMapper.deleteSubDevice(reqDTO);
    }

    @Transactional
    @Override
    public void insertSubDevice(SubDevice reqDTO) {
        //判断资产编号是否不为空且重复
        String assetNo = reqDTO.getAssetNo();
        if (StringUtils.isNotEmpty(assetNo) && StringUtils.isNotEmpty(subDeviceMapper.queryRepeatNo(assetNo))) {
            throw new ServiceException("新增时，资产编号不可重复");
        }
        // 检查设备 SN 是否重复
        if (subDeviceMapper.queryRepeatSn(reqDTO.getDeviceSn()) > 0) {
            throw new ServiceException("新增时，设备 SN 不可重复");
        }

        // 检查设备名是否重复
        if (StringUtils.isNotEmpty(subDeviceMapper.querySubRepeatByName(reqDTO.getDeviceName()))) {
            throw new ServiceException("新增时，设备名不可重复");
        }
        reqDTO.setId(IdUtils.fastSimpleUUID());
        reqDTO.setCreateTime(new Date());
        reqDTO.setCreateBy(SecurityUtils.getUsername());
        reqDTO.setStatus("0");
        reqDTO.setDeviceTag("1");
        subDeviceMapper.insetSubDevice(reqDTO);
    }

    @Transactional
    @Override
    public void updateSubDevice(SubDevice reqDTO){
        // 检查资产编号是否为空且不重复
        String assetNo = reqDTO.getAssetNo();
        if (StringUtils.isNotEmpty(assetNo)) {
            String repeatRecord = subDeviceMapper.queryRepeatNo(assetNo);
            if (repeatRecord != null && !repeatRecord.equals(reqDTO.getId())) {
                throw new ServiceException("更新时，资产编号不可重复");
            }
        }

        // 检查设备名是否重复
        String repeatIdByName = subDeviceMapper.querySubRepeatByName(reqDTO.getDeviceName());
        if (repeatIdByName != null && !repeatIdByName.equals(reqDTO.getId())) {
            throw new ServiceException("更新时，设备名不可重复");
        }
        reqDTO.setUpdateTime(new Date());
        reqDTO.setUpdateBy(SecurityUtils.getUsername());
        subDeviceMapper.updateSubDevice(reqDTO);
    }

    @Override
    public void updateSubDeviceStatus(String deviceSn, String status) {
        SubDevice subDevice = new SubDevice();
        subDevice.setDeviceSn(deviceSn);
        subDeviceMapper.updateStatusBySn(deviceSn, status);
    }

    @Override
    public List<SubDevice> subDeviceListByMainDeviceSn(String mainDeviceSn) {
        return subDeviceMapper.selectListByMainDeviceSn(mainDeviceSn);
    }

    @Override
    public List<SubDeviceListRespDTO> querySubDeviceList(AssetAccountReqDTO reqDTO) {
        if (StringUtils.isNotEmpty(reqDTO.getBeginTime())) {
            reqDTO.setBeginTime(reqDTO.getBeginTime() + " 00:00:00");
        }
        if (StringUtils.isNotEmpty(reqDTO.getEndTime())) {
            reqDTO.setEndTime(reqDTO.getEndTime() + " 23:59:59");
        }

        PageHelper.startPage(reqDTO.getPageNum(), reqDTO.getPageSize());
        List<SubDeviceListRespDTO> codeList = subDeviceMapper.querySubAccountList(reqDTO);
        for (SubDeviceListRespDTO respDTO : codeList) {
            respDTO.setLinkDeviceSn(subDeviceMapper.querySubDeviceLinkBySn(respDTO.getDeviceSn(), respDTO.getId()));
            respDTO.setOrgName("默认组织");
        }
        return codeList;
    }

}
